package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.system.domain.BedHealthDailyData;
import com.qinglei.recoup.system.dao.BedHealthDailyDataMapper;
import com.qinglei.recoup.system.pojo.SleepDataVO;
import com.qinglei.recoup.system.pojo.SleepReportVO;
import com.qinglei.recoup.system.service.BedHealthDailyDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author syp
 */
@Service
@AllArgsConstructor
public class BedHealthDailyDataServiceImpl extends ServiceImpl<BedHealthDailyDataMapper, BedHealthDailyData> implements BedHealthDailyDataService {

    /**
     * 查询单天数据
     * @param bedId
     * @param startDay
     * @return
     */
    @Override
    public SleepReportVO getSleepReportOne(Integer bedId, LocalDate startDay) {
        SleepReportVO sleepReportVO = new SleepReportVO();
        BedHealthDailyData bedHealthDailyData = getBaseMapper().selectOne(Wrappers.<BedHealthDailyData>lambdaQuery()
                .eq(BedHealthDailyData::getBedId, bedId)
                .eq(BedHealthDailyData::getStatisticsDate, startDay));
        if (bedHealthDailyData != null ) {
            if (bedHealthDailyData.getSleepData() != null) {
                List<SleepDataVO> sleepDataVOList = new ArrayList<>();
                SleepDataVO sleepDataVO1 = new SleepDataVO();
                SleepDataVO sleepDataVO2 = new SleepDataVO();
                SleepDataVO sleepDataVO3 = new SleepDataVO();
                SleepDataVO sleepDataVO4 = new SleepDataVO();
                SleepDataVO sleepDataVO5 = new SleepDataVO();
                List<String> list1 = new ArrayList<>();
                List<String> list2 = new ArrayList<>();
                List<String> list3 = new ArrayList<>();
                List<String> list4 = new ArrayList<>();
                List<String> list5 = new ArrayList<>();
                List<Integer> sleepData = JsonUtils.jsonToList(bedHealthDailyData.getSleepData(), Integer.class);
                for (Integer data : sleepData) {
                    if(data == null ){
                        continue;
                    }

                    if (data == -1) {
                        list1.add("-");
                        list2.add("-");
                        list3.add("-");
                        list4.add("-");
                        list5.add("-");
                    } else if (data == 1) {
                        list1.add("1");
                        list2.add("-");
                        list3.add("-");
                        list4.add("-");
                        list5.add("0");
                    } else if (data == 2) {
                        list1.add("-");
                        list2.add("1");
                        list3.add("-");
                        list4.add("-");
                        list5.add("1");
                    } else if (data == 3) {
                        list1.add("-");
                        list2.add("-");
                        list3.add("1");
                        list4.add("-");
                        list5.add("2");
                    } else if (data == 4) {
                        list1.add("-");
                        list2.add("-");
                        list3.add("-");
                        list4.add("1");
                        list5.add("3");
                    }
                }
                sleepDataVO1.setSleepType(1);
                sleepDataVO2.setSleepType(2);
                sleepDataVO3.setSleepType(3);
                sleepDataVO4.setSleepType(4);
                sleepDataVO5.setSleepType(5);
                sleepDataVO1.setDatas(list1);
                sleepDataVO2.setDatas(list2);
                sleepDataVO3.setDatas(list3);
                sleepDataVO4.setDatas(list4);
                sleepDataVO5.setDatas(list5);
                sleepDataVOList.add(sleepDataVO1);
                sleepDataVOList.add(sleepDataVO2);
                sleepDataVOList.add(sleepDataVO3);
                sleepDataVOList.add(sleepDataVO4);
                sleepDataVOList.add(sleepDataVO5);
                sleepReportVO.setSleepData(sleepDataVOList);
            }
            if (bedHealthDailyData.getHeartPhaseData() != null){
                sleepReportVO.setHeartPhaseData(JsonUtils.jsonToList(bedHealthDailyData.getHeartPhaseData(), Integer.class));
            }
            if (bedHealthDailyData.getBreathPhaseData() != null){
                sleepReportVO.setBreathPhaseData(JsonUtils.jsonToList(bedHealthDailyData.getBreathPhaseData(), Integer.class));
            }
            if (bedHealthDailyData.getTurnOverData() != null){
                sleepReportVO.setTurnOverData(JsonUtils.jsonToList(bedHealthDailyData.getTurnOverData(), Integer.class));
            }
            sleepReportVO.setCutStIdx(bedHealthDailyData.getCutStIdx());
            sleepReportVO.setCutEdIdx(bedHealthDailyData.getCutEdIdx());
        }
        return sleepReportVO;
    }
}
